package com.sc.weixin.uploadFile;


import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.sc.dal.model.member.Member;
import com.sc.service.member.MemberService;
import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.DataUtil;
import com.sc.util.data.FileUtil;
import com.sc.util.exception.ExceptionEnum;
/**
 * 
 * 个人中心
 * 图像上传
 * @author xiongyong
 * @2017.7.1 9:20:34 AM
 * 
 */
@Controller
@RequestMapping(value="/uploadImage")
public class UploadImageController {
	private final Logger logger = LoggerFactory.getLogger(UploadImageController.class); 
	
	/**引入用户业务层*/
	@Autowired
	private MemberService memberService;
	/****
	 * 微信-上传个人图像
	 * @return
	 */
	@RequestMapping(value="/upPersonalImage",method=RequestMethod.POST)
	@ResponseBody
	public Object upTest(MultipartHttpServletRequest requests,HttpServletRequest request){
		Member member=new Member();
		String memberId = request.getParameter("memberId");
		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();	
		String commentPath = "memberhead"; //图片存储相对路径
		try{
			if(requests.getFiles("files")  == null){
				return null;
			}
			 //取出一个list的multipartfile 
			String imgUrl = "";
	        List<MultipartFile> files = requests.getFiles("myfiles");  
	        if(files!=null && files.size()>0){
	        	MultipartFile multipartFile = files.get(0);
	            imgUrl  =  FileUtil.upload(multipartFile, "", commentPath, ConfigBeanAdapter.fileSaveAddress);
	        }
	        //取出一个list的multipartfile 
	        List<MultipartFile> files2 = requests.getFiles("myfiles2");  
	        if(files2!=null && files2.size()>0){
	        	MultipartFile multipartFile = files2.get(0);
	            imgUrl  =  FileUtil.upload(multipartFile, "", commentPath, ConfigBeanAdapter.fileSaveAddress);
	        }
	        if(imgUrl!=null&&!"".equals(imgUrl)&&!"1".equals(imgUrl)&&!"2".equals(imgUrl)){				
				member.setmImg(imgUrl);
			}
	        member.setmId(memberId);
			member.setModifyTime(new Date());
			int flag = memberService.updateByPrimaryKeySelective(member);
			if(flag > 0){			
				//System.out.println("上传成功！！！！！！！！");
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_FILE_ERROR);
			}
						
		} catch(Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_FILE_ERROR);
		}
		return resultMap;
	}


}
